package util;

import hibernate.mappings.MsNode;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

public class SocketUtil {
	private static int number =3672;//

	public String getDocumentAt(String urlString) {
		StringBuffer document = new StringBuffer();
		try {
			URL url = new URL(urlString);
			URLConnection conn = url.openConnection();
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					conn.getInputStream(), "UTF-8"));
			String line = null;
			while ((line = reader.readLine()) != null)
				document.append(line + "\n");
			reader.close();

		} catch (MalformedURLException e) {
			System.out.println("Unable to connect to URL: " + urlString);
		} catch (IOException e) {
			System.out.println("IOException when connecting to URL: "
					+ urlString);
		}
		return document.toString();
	}

	public int download(String num, String title) {// if(true)return ;
		File dir = new File("D:/1/" + title + num);
		if (!dir.exists())
			dir.mkdirs();
		// StringBuffer document = new StringBuffer();
		int i = 1;// http://img1.airenti.net/photoH/2632/30.jpg
		String urlString = "http://img1.airenti.org/photoI/" + num + "/";
		for (; i < 100; i++) {
			String tempurl = urlString;
			if (i < 10)
				tempurl = tempurl + "0";
			tempurl = tempurl + i + ".jpg";
			URL url;
			try {
				// BufferedReader reader = new BufferedReader(new
				// InputStreamReader(
				// conn.getInputStream()));
				File file = new File("D:/1/" + title + num + "/" + title + i
						+ ".jpg");
				/*
				 * if (!file.exists()) { file.createNewFile(); }else continue;
				 */
				url = new URL(tempurl);
				// URLConnection conn = url.openConnection();
				InputStream in = url.openStream();
				BufferedInputStream bufIn = new BufferedInputStream(in);
				BufferedOutputStream bufOut = new BufferedOutputStream(
						new FileOutputStream(file));
				byte[] b = new byte[1];
				while (bufIn.read(b) != -1) {
					bufOut.write(b);
				}
				bufOut.flush();
				bufOut.close();
				bufIn.close();
				if (file.length() <= 69634l) {
					file.delete();
					if(i==1)dir.delete();
					break;
				}
			} catch (Exception e) {
				return i;
			}
		}
		return i;

	}

	public static void main(String[] args) {
		SocketUtil client = new SocketUtil();
		List list = new ArrayList();
		for (int i = 1; i < 99; i++) {
			List list1 = client.getTopic(i);
			if (list1 == null)
				break;
			list.addAll(list1);
			if (list1.size() < 30) {
				break;
			}
		}
		int total = 0;
		System.out.println("最大值是 " + list.get(list.size()-1) + "  总共：" + (total = list.size()));
		for (int i = total - 1; i >= 0; i--) {
			MsNode m = (MsNode) list.get(i);
			int num = m.getId();
			String title = m.getTitle();
			client.download(num + "", title);
			System.out.println("完成：" + num + title + " 还有" + (i));
		}

	}

	public List getTopic(int page) {
		SocketUtil client = new SocketUtil();
		List list = new ArrayList();
		String url = "http://www.airenti.org/Html/Type/1_" + page + ".html";
		String examda = client.getDocumentAt(url);
		int index = examda.indexOf("<ul class=\"image\">");
		String temp = "";
		if (index == -1) {
			System.out.println("no image");
		} else {

			String imgs = examda.substring(index);
			int number0 = number;
			String strArray[] = imgs.split("<li>");
			int total = strArray.length;
			for (int i = 1; i < strArray.length; i++) {
				temp = strArray[i];
				index = temp.indexOf("/Images/");
				int index2 = temp.indexOf(".jpg\" alt=\"");
				if (index > 0 && index2 > 0) {
					String num = temp.substring(index + 8, index2);
					int num0 = Integer.parseInt(num);
					if (num0 <= number) {
						continue;
					}
					if (num0 > number0) {
						number0 = num0;
					}
					String title = "notitle";
					temp = temp.substring(index2 + 11);
					int index3 = temp.indexOf("\"");
					if (index3 > 0)
						title = temp.substring(0, index3);
					MsNode m = new MsNode();
					m.setId(num0);
					m.setTitle(title);
					list.add(m);

					// int state=client.download(num,title);
					// if(state==1)break;
					// System.out.println("完成："+num+" "+title+" 还有"+(total-i));
				}
			}
		}

		return list;

	}
}
